package org.wiztools.te.plugin;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.wiztools.te.TEMod;
import org.wiztools.te.TEProcess;

public class ReplaceAll implements TEProcess{
	private TEMod tem;
	private JTextField jtf_re, jtf_replace;
	private JButton jb_replace, jb_cancel;
	private Container ct;
	
	public void setTEMod(TEMod tem){
		this.tem = tem;
	}
	
	public void init(){
		jtf_re = new JTextField(15);
		jtf_replace = new JTextField(15);
		jb_replace = new JButton("ReplcaeAll");
		jb_replace.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					if(jtf_re.getText().equals("") || jtf_replace.getText().equals("")){
						Toolkit.getDefaultToolkit().beep();
						return;
					}
					String str = tem.getText();
					str = str.replaceAll(jtf_re.getText(), jtf_replace.getText());
					tem.setText(str);
					tem.closeDialog();
				}
			}
		);
		jb_cancel = new JButton("Cancel");
		jb_cancel.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent ae){
					tem.closeDialog();
				}
			}
		);
		
		ct = new JPanel();
		ct.setLayout(new BorderLayout());

		JPanel jp_center = new JPanel();
		jp_center.setLayout(new GridLayout(2,2));
		jp_center.add(new JLabel("RE"));
		jp_center.add(jtf_re);
		jp_center.add(new JLabel("ReplaceWth:"));
		jp_center.add(jtf_replace);
		ct.add(jp_center, BorderLayout.CENTER);

		JPanel jp_south = new JPanel();
		jp_south.setLayout(new FlowLayout());
		jp_south.add(jb_replace);
		jp_south.add(jb_cancel);
		ct.add(jp_south, BorderLayout.SOUTH);
	}
	
	public void action(){
		tem.dispDialog(ct, "ReplaceAll");
		jtf_re.selectAll();
	}
	
	public void destroy(){
		// do nothing!
	}
}
